/**
 * @brief 用户程序
 */
#include "user_app.h"
#include "lib.h"
#include "flash.h"
#include "hem_port.h"
#include "rd_protocol.h"
#include "drv_usart2.h"
#include "mb_master_app.h"
uint8_t  uc_activateWarning_timer_start = 0;/*启动预警定时器开始标志*/
uint32_t ul_activateWarning_timer_cnt   = 0;/*启动预警定时器计数，1ms加1*/

uint8_t uc_slope_timer_start = 0;
uint32_t ul_slope_timer_cnt  = 0;

/**
 * @brief 
 */
void user_timer_update(void)
{
    Lib_Timer_Counter(&ul_activateWarning_timer_cnt,uc_activateWarning_timer_start);
    Lib_Timer_Counter(&ul_slope_timer_cnt,uc_slope_timer_start);
}
/**
 * @brief 用户程序定时器
 */
void user_app_tick(uint32_t tick)
{
    if((tick%2) == 0) 
    {
        user_timer_update();
    }
}

/**********************************报警值******************************/
float uf_gasConcentration_alarmValue       = 1.0f;//瓦斯浓度报警值
float uf_hydraulicOilTemperature_alarmValue= 70.0f;//液压油油温报警值
float uf_engineWaterTemperature_alarmValue = 70.0f;//发动机水温报警值
float uf_engineTemperature_alarmValue      = 70.0f;//发动机表温报警值
float uf_exhaustGasTemperature_alarmValue  = 70.0f;//发动机排气温度报警值
float uf_mainPumpPressure_alarmValue       = 29.8f;//主泵压力报警值
float uf_fanPressure_alarmValue            = 29.8f;//风扇压力报警值
float uf_brakePressure_alarmValue          = 29.8f;//刹车压力报警值
/**
 * @brief 存储报警值,可放到500ms的任务中
 */
void save_alarmValue(void)
{
    param_t tParam;
    param_t tReadParam;
    int temp = 0;
    uint16_t length = 0;

    tParam.params.gasConcentration_alarmValue       = uf_gasConcentration_alarmValue;
    tParam.params.hydraulicOilTemperature_alarmValue= uf_hydraulicOilTemperature_alarmValue;
    tParam.params.engineWaterTemperature_alarmValue = uf_engineWaterTemperature_alarmValue;
    tParam.params.engineTemperature_alarmValue      = uf_engineTemperature_alarmValue;
    tParam.params.exhaustGasTemperature_alarmValue  = uf_exhaustGasTemperature_alarmValue;
    tParam.params.mainPumpPressure_alarmValue       = uf_mainPumpPressure_alarmValue;
    tParam.params.fanPressure_alarmValue            = uf_fanPressure_alarmValue;
    tParam.params.brakePressure_alarmValue          = uf_brakePressure_alarmValue;

    length = sizeof(param_t) / sizeof(uint32_t);

    Flash_read_32bit_data(USER_DATA_ADDR_BASE,length,(int32_t*)tReadParam.buf);//读出存储的参数数据
    temp = memcmp(tParam.buf,tReadParam.buf,length);
    if(temp != 0)
    {
        //报警值是小数，需要存储小数
        Flash_erase_page(USER_DATA_ADDR_BASE);
        Flash_write_32bit_data_with_sector_erased(USER_DATA_ADDR_BASE,length,tParam.buf);
    }
}
/**
 * @brief 载入设置的参数，包括报警值
 */
void load_param(void)
{
    param_t tReadParam;
	  uint16_t length = 0;
	
    Flash_read_32bit_data(USER_DATA_ADDR_BASE,length,(int32_t *)tReadParam.buf);//读出存储的参数数据
    //以下参数在读取数据的时候需要进行合理性判断
    if(tReadParam.buf[0] == 0xFFFFFFFF)
    {
        uf_gasConcentration_alarmValue = 1.0f;
    }
    else
    {
        uf_gasConcentration_alarmValue = tReadParam.params.gasConcentration_alarmValue;
    }
    if(tReadParam.buf[1] == 0xFFFFFFFF)
    {
        uf_hydraulicOilTemperature_alarmValue = 70.0f;
    }
    else
    {
        uf_hydraulicOilTemperature_alarmValue = tReadParam.params.hydraulicOilTemperature_alarmValue;
    }
    
    if(tReadParam.buf[2] == 0xFFFFFFFF)
    {
        uf_engineWaterTemperature_alarmValue = 70.0f;
    }
    else
    {
        uf_engineWaterTemperature_alarmValue = tReadParam.params.engineWaterTemperature_alarmValue;
    }
    if(tReadParam.buf[3] == 0xFFFFFFFF)
    {
        uf_engineTemperature_alarmValue = 70.0f;
    }
    else
    {
        uf_engineTemperature_alarmValue = tReadParam.params.engineTemperature_alarmValue;
    }
    if(tReadParam.buf[4] == 0xFFFFFFFF)
    {
        uf_exhaustGasTemperature_alarmValue = 70.0f;
    }
    else
    {
        uf_exhaustGasTemperature_alarmValue = tReadParam.params.exhaustGasTemperature_alarmValue;
    }
    if(tReadParam.buf[5] == 0xFFFFFFFF)
    {
        uf_mainPumpPressure_alarmValue = 29.8f;
    }
    else
    {
        uf_mainPumpPressure_alarmValue = tReadParam.params.mainPumpPressure_alarmValue;
    }
    if(tReadParam.buf[6] == 0xFFFFFFFF)
    {
        uf_fanPressure_alarmValue = 29.8f;
    }
    else
    {
        uf_fanPressure_alarmValue = tReadParam.params.fanPressure_alarmValue;
    }
    if(tReadParam.buf[7] == 0xFFFFFFFF)
    {
        uf_brakePressure_alarmValue = 29.8f;
    }
    else
    {
        uf_brakePressure_alarmValue = tReadParam.params.brakePressure_alarmValue;
    }
}
uint8_t uc_oilOut_flag_do       = 0;/*KA1-油门输出标志位*/
uint8_t uc_fault_alarm_do       = 0;/*KA2-故障报警 */
uint8_t uc_activate_warning_do  = 0;/*KA3-启动预警 */
uint8_t uc_preActive_warning_do = 0;/*KA4-动作前预警*/
uint8_t uc_device_start_do      = 0;/*KA5-设备启动 */
uint8_t uc_device_stop_do       = 0;/*KA6-设备停止 */
uint8_t uc_device_start_flag_do = 0;/*KA7-启动标志位*/

uint16_t us_forward_pwm_1 = 0;/*1BDT-前进1*/
uint16_t us_retreat_pwm_1 = 0;/*2BDT-后退1*/
uint16_t us_forward_pwm_2 = 0;/*3BDT-前进2*/
uint16_t us_retreat_pwm_2 = 0;/*4BDT-后退2*/

uint16_t us_brake_pwm     = 0;/*5BDT-刹车*/
uint16_t us_fan_pwm       = 0;/*6BDT-风扇*/

uint16_t us_oil_out1_ao   = 0;/*油门输出1,0.55V~2.1V*/
uint16_t us_oil_out2_ao   = 0;/*油门输出2,1.1V~4.2V*/
/**
 * @brief 输出重定义
 */
void out_redefine(void)
{
    gp_io->dout[0]  = uc_oilOut_flag_do;
    gp_io->dout[1]  = uc_fault_alarm_do;
    gp_io->dout[2]  = uc_activate_warning_do;
    gp_io->dout[3]  = uc_preActive_warning_do;
    gp_io->dout[4]  = uc_device_start_do;
    gp_io->dout[5]  = uc_device_stop_do;
    gp_io->dout[6]  = uc_device_start_flag_do;

    gp_io->pwm[0] = us_forward_pwm_1;
    gp_io->pwm[1] = us_retreat_pwm_1;
    gp_io->pwm[2] = us_forward_pwm_2;
    gp_io->pwm[3] = us_retreat_pwm_2;
    gp_io->pwm[4] = us_brake_pwm;
    gp_io->pwm[5] = us_fan_pwm;

    gp_io->ao[0] = us_oil_out1_ao;
    gp_io->ao[1] = us_oil_out2_ao;
}


float uf_gasConcentration       = 0.0f;//瓦斯浓度
float uf_hydraulicOilTemperature= 0.0f;//液压油油温
float uf_engineWaterTemperature = 0.0f;//发动机水温
float uf_engineTemperature      = 0.0f;//发动机表温
float uf_exhaustGasTemperature  = 0.0f;//发动机排气温度
float uf_mainPumpPressure       = 0.0f;//主泵压力
float uf_fanPressure            = 0.0f;//风扇压力
float uf_brakePressure          = 0.0f;//刹车压力

uint8_t uc_diLowLiquidLevelDetectionFirstIn = 0;//低液位检测1
uint8_t uc_diLowLiquidLevelDetectionSecondIn= 0;//低液位检测2
uint8_t uc_diRadioChooseIn                  = 0;//遥控
uint8_t uc_diRemoteChooseIn                 = 0;//远程

uint8_t uc_diStartIn                        = 0;//启动
uint8_t uc_diStopIn                         = 0;//停止
uint8_t uc_diStartInLast                    = 0;//启动
uint8_t uc_diStopInLast                     = 0;//停止
uint8_t uc_StartInCmd                       = 0;//启动
uint8_t uc_StopInCmd                        = 0;//停止

uint8_t uc_diModbus_rtu_isValid             = 0;/*0:自定义协议；1：modbus485 RTU*/
uint8_t uc_diModbus_rtu_isValidLast         = 0xFF;/*0:自定义协议；1：modbus485 RTU*/

/**
 * @brief 控制器输入重定义
 */
void di_data_redefine(void)
{
    uint16_t t16 = 0;
    uint8_t t8 = 0;
    float tf = 0.0f;
    float tfBuf[4] = { 0.0f };
    uint8_t i = 0;

    t16 = gp_io->ai[0];//瓦斯浓度
    if(t16 >= 4000)
    {
        tf = (4/16)*(t16-4000);
    }
    uf_gasConcentration = tf/1000;
    //温度传感器，液压油油温，发动机水温，发动机表温，发动机排气温度
    for(i = 0;i < 4;i++)
    {
        t16 = gp_io->ai[1+i];
        if(t16 >= 4000)
        {
            tf = (100/16)*(t16 - 4000);
        }
        tfBuf[i] = tf/1000;
    }
    uf_hydraulicOilTemperature = tfBuf[0];//液压油油温
    uf_engineWaterTemperature  = tfBuf[1];//发动机水温
    uf_engineTemperature       = tfBuf[2];//发动机表温
    uf_exhaustGasTemperature   = tfBuf[3];//发动机排气温度
    /**压力数据处理 */
    for(i = 0;i < 3;i++)
    {
        t16 = gp_io->ai[5+i];
        if(t16 >= 4000)
        {
            tf = (30/16)*(t16 - 4000);
        }
        tfBuf[i] = tf/4000;
    }
    uf_mainPumpPressure = tfBuf[0];//主泵压力
    uf_fanPressure      = tfBuf[1];//风扇压力
    uf_brakePressure    = tfBuf[2];//刹车压力

    uc_diLowLiquidLevelDetectionFirstIn = gp_io->di[18];//低液位检测1
    uc_diLowLiquidLevelDetectionSecondIn= gp_io->di[19];//低液位检测2
    uc_diRadioChooseIn                  = gp_io->di[20];//本地
    uc_diRemoteChooseIn                 = gp_io->di[21];//远程
    uc_diStartIn                        = gp_io->di[22];//启动
    uc_diStopIn                         = gp_io->di[23];//停止

    uc_diModbus_rtu_isValid             = gp_io->di[26];//协议选择
    //发动机启动
    if((uc_diStartIn == 1) || (uc_rd_s12 == 1))
    {
        t8 = 1;
    }
    else
    {
        t8 = 0;
    }
    Lib_LockUnlockDataHandle(&uc_diStartInLast,&t8,&uc_StartInCmd,SelfReset);
    //发动机停止
    if((uc_diStopIn == 1) || (uc_rd_s13 == 1))
    {
        t8 = 1;
    }
    else
    {
        t8 = 0;
    }
    Lib_LockUnlockDataHandle(&uc_diStopInLast,&t8,&uc_StopInCmd,SelfReset);
}
/**
 * @brief 报警，置位标志位
 */
uint8_t uc_alarm_sn = 0;/*报警标志 */
void user_alarm_handler(void)
{
    if((uf_gasConcentration        > uf_gasConcentration_alarmValue)       || 
       (uf_hydraulicOilTemperature > uf_hydraulicOilTemperature_alarmValue)|| 
       (uf_engineWaterTemperature  > uf_engineWaterTemperature_alarmValue) ||
       (uf_engineTemperature       > uf_engineTemperature_alarmValue)      ||
       (uf_exhaustGasTemperature   > uf_exhaustGasTemperature_alarmValue)  ||
       (uf_mainPumpPressure        > uf_mainPumpPressure_alarmValue)       ||
       (uf_fanPressure             > uf_fanPressure_alarmValue)            ||
       (uf_brakePressure           > uf_brakePressure_alarmValue)          ||
       (uc_diLowLiquidLevelDetectionFirstIn == 1) ||
       (uc_diLowLiquidLevelDetectionSecondIn == 1)
    )
    {
        uc_alarm_sn = 1;
    }
    else
    {
        uc_alarm_sn = 0;
    }

    if(uc_alarm_sn == 1)
    {
        uc_fault_alarm_do = 1;
    }
    else
    {
        uc_fault_alarm_do = 0;
    }
}
/**
 * @brief 根据端口电压不同对rs485初始化为不同的波特率
 * 自由协议波特率4800；modbus RTU 波特率是19200
 */
void user_app_rs485_initHandler(void)
{
    if(uc_diModbus_rtu_isValid == uc_diModbus_rtu_isValidLast) return;

    uc_diModbus_rtu_isValidLast = uc_diModbus_rtu_isValid;
    if(uc_diModbus_rtu_isValid == 0)//自定义协议，波特率4800
    {
        RS485_init(4800);
    }
    else if(uc_diModbus_rtu_isValid == 1)
    {
        RS485_init(19200);
    }
}
/**
 * @brief  modbus_rtu
 * @param  
 * @retval 
 */
#define XF_VALUE_REG       0x0000    //寄存器地址
#define XF_VALUE_REG_NUM   11        //寄存器地址数量
uint8_t g_slave_addr = 1;/*从机地址 */
uint8_t g_reg_number = XF_VALUE_REG_NUM;
uint16_t g_reg_addr  = XF_VALUE_REG;
/**
 * @brief modbus485 rtu 作为主机读取数据
 */
void m_modbus_rtu(void)
{
    if(uc_diModbus_rtu_isValid == 1)
    {
        MB_M_Read_HoldingReg_Frame(g_slave_addr,g_reg_addr,g_reg_number);//鑫丰测试，寄存器地址从0开始；寄存器数量11个，已测试OK
    }
}
uint8_t uc_mdRtu_enmergency_stop = 0;/*急停*/
uint8_t uc_mdRtu_online          = 0;/*在线标志*/
uint8_t uc_mdRtu_heartbeatFlag   = 0;/*心跳标志*/

uint8_t uc_mdRtu_dialButton_1up         = 0;/*拨钮1-上，风扇打开*/
uint8_t uc_mdRtu_dialButton_1down       = 0;/*拨钮1-下，风扇关闭*/
uint8_t uc_mdRtu_dialButton_2up         = 0;/*拨钮2-上，自动前进*/
uint8_t uc_mdRtu_dialButton_2down       = 0;/*拨钮2-下，自动后退*/
uint8_t uc_mdRtu_dialButton_3up         = 0;/*拨钮3-上*/
uint8_t uc_mdRtu_dialButton_3down       = 0;/*拨钮3-下*/
uint8_t uc_mdRtu_dialButton_4up         = 0;/*拨钮4-上 */
uint8_t uc_mdRtu_dialButton_4down       = 0;/*拨钮4-下*/
uint8_t uc_mdRtu_dialButton_5up         = 0;/*拨钮5-上 */
uint8_t uc_mdRtu_dialButton_5down       = 0;/*拨钮5-下*/
uint8_t uc_mdRtu_dialButton_6up         = 0;/*拨钮6-上 */
uint8_t uc_mdRtu_dialButton_6down       = 0;/*拨钮6-下*/
uint8_t uc_mdRtu_dialButton_7up         = 0;/*拨钮7-上 */
uint8_t uc_mdRtu_dialButton_7down       = 0;/*拨钮7-下*/
uint8_t uc_mdRtu_dialButton_8up         = 0;/*拨钮8-上 */
uint8_t uc_mdRtu_dialButton_8down       = 0;/*拨钮8-下*/
uint8_t uc_mdRtu_dialButton_9up         = 0;/*拨钮9-上 */
uint8_t uc_mdRtu_dialButton_9down       = 0;/*拨钮9-下*/
uint8_t uc_mdRtu_dialButton_10up        = 0;/*拨钮10-上 */
uint8_t uc_mdRtu_dialButton_10down      = 0;/*拨钮10-下*/
uint8_t uc_mdRtu_dialButton_back_1up    = 0;/*拨钮备用1-上 */
uint8_t uc_mdRtu_dialButton_back_1down  = 0;/*拨钮备用1-下*/
uint8_t uc_mdRtu_dialButton_back_2up    = 0;/*拨钮备用2-上，发动机启动*/
uint8_t uc_mdRtu_dialButton_back_2down  = 0;/*拨钮备用2-下，发动机熄火*/

uint8_t us_mdRtu_j1_up_down    = 0x80;/*j1上下输入*/
uint8_t us_mdRtu_j1_left_right = 0x80;/*j1左右输入*/
uint8_t us_mdRtu_j2_up_down    = 0x80;/*j2上下输入*/
uint8_t us_mdRtu_j2_left_right = 0x80;/*j2左右输入*/
uint8_t us_mdRtu_j3_up_down    = 0x80;/*j3上下输入*/
uint8_t us_mdRtu_j3_left_right = 0x80;/*j3左右输入*/

uint8_t us_mdRtu_Potentiometer = 0;/*电位计输入 */
/**
 * @brief 清除modbus RTU解析的参数
 */
void clear_mdRtu_data(void)
{
    uc_mdRtu_enmergency_stop = 0;
    uc_mdRtu_online          = 0;
    uc_mdRtu_heartbeatFlag   = 0;

    uc_mdRtu_dialButton_1up  = 0;
    uc_mdRtu_dialButton_1down= 0;
    uc_mdRtu_dialButton_2up  = 0;
    uc_mdRtu_dialButton_2down= 0;
    uc_mdRtu_dialButton_3up  = 0;
    uc_mdRtu_dialButton_3down= 0;
    uc_mdRtu_dialButton_4up  = 0;
    uc_mdRtu_dialButton_4down= 0;
    uc_mdRtu_dialButton_5up  = 0;
    uc_mdRtu_dialButton_5down= 0;
    uc_mdRtu_dialButton_6up  = 0;
    uc_mdRtu_dialButton_6down= 0;
    uc_mdRtu_dialButton_7up  = 0;
    uc_mdRtu_dialButton_7down= 0;
    uc_mdRtu_dialButton_8up  = 0;
    uc_mdRtu_dialButton_8down= 0;
    uc_mdRtu_dialButton_9up  = 0;
    uc_mdRtu_dialButton_9down= 0;
    uc_mdRtu_dialButton_10up = 0;
    uc_mdRtu_dialButton_10down= 0;
    uc_mdRtu_dialButton_back_1up  = 0;
    uc_mdRtu_dialButton_back_1down= 0;
    uc_mdRtu_dialButton_back_2up  = 0;
    uc_mdRtu_dialButton_back_2down= 0;

    us_mdRtu_j1_up_down    = 0x80;
    us_mdRtu_j1_left_right = 0x80;
    us_mdRtu_j2_up_down    = 0x80;
    us_mdRtu_j2_left_right = 0x80;
    us_mdRtu_j3_up_down    = 0x80;
    us_mdRtu_j3_left_right = 0x80;

    us_mdRtu_Potentiometer = 0;
}
/**
 * @brief 摇杆模拟量数据解析,将0~128解析到0~100；上和左，下和右解析方式相同；
 * param[in] src 摇杆输入原始值，中位值是0x80,范围0x00<-->0x80<-->0xFF
 * param[out] *up_left 输出上或者左的值，范围是0~100；
 * param[out] *down_right 输出下或者右的值，范围是0~100
 */
void rocker_dataAnalysis(uint8_t src,uint8_t *up_left,uint8_t *down_right)
{
    float coefficient = (100.0f/128.0f);
    uint8_t t8 = 0;
    float tf = 0;
    
    if(src < 0x80)      
    {
        t8 = (0x80-src);
        tf = t8 * coefficient;
        *up_left = tf;
        *down_right = 0;
    }
    else if(src > 0x80) 
    {
        t8 = (src - 0x80 + 1);
        tf = t8 * coefficient;
        *up_left = 0;
        *down_right = tf;
    }
    else                
    {
        *up_left = 0;
        *down_right = 0;
    }
}
/**
 * @brief modbus485 rtu 数据解析
 *        rtu的数据已经读取到保持寄存器中
 */
void m_modbus_rtu_dataAnalysis(void)
{
    uint16_t t16 = 0;
    /*只有选择mobus RTU的设备和选择遥控控制，才进行数据处理 */
    if((uc_diModbus_rtu_isValid == 0) || (uc_diRadioChooseIn == 0))
    {
        clear_mdRtu_data();
        return;
    }
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,0);//急停，发射器在线，心跳
    uc_mdRtu_enmergency_stop = (t16>>0)&0x01;
    uc_mdRtu_online          = (t16>>1)&0x01;
    uc_mdRtu_heartbeatFlag   = (t16>>4)&0x01;

    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,1);//
    uc_mdRtu_dialButton_1up  = (t16>>0)&0x01;
    uc_mdRtu_dialButton_1down= (t16>>1)&0x01;
    uc_mdRtu_dialButton_2up  = (t16>>2)&0x01;
    uc_mdRtu_dialButton_2down= (t16>>3)&0x01;
    uc_mdRtu_dialButton_3up  = (t16>>4)&0x01;
    uc_mdRtu_dialButton_3down= (t16>>5)&0x01;
    uc_mdRtu_dialButton_4up  = (t16>>6)&0x01;
    uc_mdRtu_dialButton_4down= (t16>>7)&0x01;
    uc_mdRtu_dialButton_5up  = (t16>>8)&0x01;
    uc_mdRtu_dialButton_5down= (t16>>9)&0x01;
    uc_mdRtu_dialButton_6up  = (t16>>10)&0x01;
    uc_mdRtu_dialButton_6down= (t16>>11)&0x01;
    uc_mdRtu_dialButton_7up  = (t16>>12)&0x01;
    uc_mdRtu_dialButton_7down= (t16>>13)&0x01;
    uc_mdRtu_dialButton_8up  = (t16>>14)&0x01;
    uc_mdRtu_dialButton_8down= (t16>>15)&0x01;

    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,2);//
    uc_mdRtu_dialButton_9up         = (t16>>0)&0x01;
    uc_mdRtu_dialButton_9down       = (t16>>1)&0x01;
    uc_mdRtu_dialButton_10up        = (t16>>2)&0x01;
    uc_mdRtu_dialButton_10down      = (t16>>3)&0x01;
    uc_mdRtu_dialButton_back_1up    = (t16>>4)&0x01;
    uc_mdRtu_dialButton_back_1down  = (t16>>5)&0x01;
    uc_mdRtu_dialButton_back_2up    = (t16>>6)&0x01;
    uc_mdRtu_dialButton_back_2down  = (t16>>7)&0x01;

    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,3);
    us_mdRtu_j1_up_down    = (t16>>0)&0xFF;
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,4);
    us_mdRtu_j1_left_right = (t16>>0)&0xFF;
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,5);
    us_mdRtu_j2_up_down    = (t16>>0)&0xFF;
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,6);
    us_mdRtu_j2_left_right = (t16>>0)&0xFF;
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,7);
    us_mdRtu_j3_up_down    = (t16>>0)&0xFF;
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,8);
    us_mdRtu_j3_left_right = (t16>>0)&0xFF;
    t16 = MB_M_ReadOneHoldBuf(g_slave_addr,9);
    us_mdRtu_Potentiometer = (t16>>0)&0xFF;
}
uint8_t uc_mdRtu_j1_up    = 0;/*j1-上，手动前进1和前进2*/
uint8_t uc_mdRtu_j1_down  = 0;/*j1-下，手动后退1和后退2*/
uint8_t uc_mdRtu_j1_left  = 0;/*j1-左*/
uint8_t uc_mdRtu_j1_right = 0;/*j1-右*/

uint8_t uc_mdRtu_j2_up    = 0;/*j2-上*/
uint8_t uc_mdRtu_j2_down  = 0;/*j2-下*/
uint8_t uc_mdRtu_j2_left  = 0;/*j2-左*/
uint8_t uc_mdRtu_j2_right = 0;/*j2-右*/

uint8_t uc_mdRtu_j3_up    = 0;/*j3-上，手动前进1*/
uint8_t uc_mdRtu_j3_down  = 0;/*j3-下，手动后退1*/
uint8_t uc_mdRtu_j3_left  = 0;/*j3-左*/
uint8_t uc_mdRtu_j3_right = 0;/*j3-右*/
/**
 * @brief 摇杆数据处理
 */
void rocker_dataHandler(void)
{
    rocker_dataAnalysis(us_mdRtu_j1_up_down,   &uc_mdRtu_j1_up,  &uc_mdRtu_j1_down);
    rocker_dataAnalysis(us_mdRtu_j1_left_right,&uc_mdRtu_j1_left,&uc_mdRtu_j1_right);
    
    rocker_dataAnalysis(us_mdRtu_j2_up_down,   &uc_mdRtu_j2_up,  &uc_mdRtu_j2_down);
    rocker_dataAnalysis(us_mdRtu_j2_left_right,&uc_mdRtu_j2_left,&uc_mdRtu_j2_right);
    
    rocker_dataAnalysis(us_mdRtu_j3_up_down,   &uc_mdRtu_j3_up,  &uc_mdRtu_j3_down);
    rocker_dataAnalysis(us_mdRtu_j3_left_right,&uc_mdRtu_j3_left,&uc_mdRtu_j3_right);
}

/**
 * @brief modbus RTU接收的数据重定义
 */
uint8_t uc_mb_rtu_autoForwardIn = 0;/*自动前进*/
uint8_t uc_mb_rtu_autoRetreatIn = 0;/*自动后退 */
uint8_t uc_mb_rtu_fastForwardIn = 0;/*快速前进 */
uint8_t uc_mb_rtu_fastRetreatIn = 0;/*快速后退*/
uint8_t uc_mb_rtu_slowForwardIn = 0;/*慢速前进*/
uint8_t uc_mb_rtu_slowRetreatIn = 0;/*慢速后退*/
uint8_t uc_mb_rtu_fanIn         = 0;/*风扇，1-风扇打开；0-风扇关闭*/
uint8_t uc_mb_rtu_engineStartIn = 0;/*发动机启动*/ 
uint8_t uc_mb_rtu_engineStopIn  = 0;/*发动机停止*/
void m_modbus_rtu_dataRedefine(void)
{
    //自动前进后退
    if(uc_mdRtu_dialButton_2up ==1)
    {
        uc_mb_rtu_autoForwardIn = 1;
        uc_mb_rtu_autoRetreatIn = 0;
    }
    else if(uc_mdRtu_dialButton_2down == 1)
    {
        uc_mb_rtu_autoForwardIn = 0;
        uc_mb_rtu_autoRetreatIn = 1;
    }
    //快速前进后退
    if(uc_mdRtu_j1_up > 0)
    {
        uc_mb_rtu_fastForwardIn = (uc_mdRtu_j1_up * 100);
        uc_mb_rtu_fastRetreatIn = 0;
    }
    else if(uc_mdRtu_j1_down > 0)//快速后退
    {
        uc_mb_rtu_fastForwardIn = 0;
        uc_mb_rtu_fastRetreatIn = (uc_mdRtu_j1_down * 100);
    }
    else
    {
        uc_mb_rtu_fastForwardIn = 0;
        uc_mb_rtu_fastRetreatIn = 0;
    }
    //慢速前进后退
    if(uc_mdRtu_j3_up > 0)
    {
        uc_mb_rtu_slowForwardIn = (uc_mdRtu_j3_up * 100);
        uc_mb_rtu_slowRetreatIn = 0;
    }
    else if(uc_mdRtu_j3_down > 0)
    {
        uc_mb_rtu_slowForwardIn = 0;
        uc_mb_rtu_slowRetreatIn = (uc_mdRtu_j3_down * 100);
    }
    //风扇启动关闭
    if(uc_mdRtu_dialButton_1up == 1)
    {
        uc_mb_rtu_fanIn = 1;
    }
    else if(uc_mdRtu_dialButton_1down == 1)
    {
        uc_mb_rtu_fanIn = 0;
    } 
    //发动机启动停止
    if(uc_mdRtu_dialButton_back_2up == 1)
    {
        uc_mb_rtu_engineStartIn = 1;
        uc_mb_rtu_engineStopIn = 0;
    }
    else if(uc_mdRtu_dialButton_back_2down == 1)
    {
        uc_mb_rtu_engineStartIn = 0;
        uc_mb_rtu_engineStopIn = 1;
    }
}

/**
 * @brief 在遥控模式下，发动机启动
 * return 1-启动完成；2-启动异常，如在延迟1S过程中异常；
 */
uint8_t uc_engineStartInRadio_type = 0;
uint8_t engine_startInRadio(void)
{
    uint8_t tReturn = 0;
    switch(uc_engineStartInRadio_type)
    {
        case 0:
            if((uc_alarm_sn == 0) && (uc_mdRtu_enmergency_stop == 0))
            {
                if((uc_StartInCmd == 1) || (uc_mb_rtu_engineStartIn == 1))
                {
                    Lib_Timer_Start(&uc_activateWarning_timer_start);
                    uc_engineStartInRadio_type += 1;
                }
            }
            break;
        case 1:
            uc_activate_warning_do  = 1;
            uc_device_start_do      = 1;
            uc_device_start_flag_do = 1;
            
            if(ul_activateWarning_timer_cnt > TIME_OUT_1S)
            {
                Lib_Timer_Stop(&ul_activateWarning_timer_cnt,&uc_activateWarning_timer_start);
                uc_activate_warning_do = 0;
                uc_engineStartInRadio_type += 1;
            }
            if((uc_StartInCmd == 0) &&(uc_mb_rtu_engineStartIn == 0))
            {
                Lib_Timer_Stop(&ul_activateWarning_timer_cnt,&uc_activateWarning_timer_start);
                uc_activate_warning_do = 0;
                uc_device_start_do     = 0;
                uc_device_start_flag_do= 0;
                uc_engineStartInRadio_type = 0;
                tReturn = 2;
            }
            else if((uc_alarm_sn == 1) || (uc_mdRtu_enmergency_stop == 1))/*故障或者急停 */
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 1;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/

                Lib_Timer_Stop(&ul_activateWarning_timer_cnt,&uc_activateWarning_timer_start);
                tReturn = 2;
                uc_engineStartInRadio_type = 0;
            }
            break;
        case 2: 
            if((uc_StartInCmd == 0) && (uc_mb_rtu_engineStartIn == 0))
            {
                uc_device_start_do  = 0;
                uc_engineStartInRadio_type = 0;

                tReturn = 1;//启动完成
            }
            else if((uc_alarm_sn == 1) || (uc_mdRtu_enmergency_stop == 1))
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 1;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/

                uc_engineStartInRadio_type = 0;
                tReturn = 2;                /*故障或者急停*/
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 在遥控模式下发动机停止
 */
uint8_t uc_engineStopInRadio_mode = 0;
uint8_t engine_stopInRadio(void)
{
    uint8_t tReturn = 0;

    switch(uc_engineStopInRadio_mode)
    {
        case 0:
            if((uc_StopInCmd            == 1) || 
               (uc_diRadioChooseIn      == 0) || 
               (uc_diRemoteChooseIn     == 1) ||
               (uc_mb_rtu_engineStopIn  == 1)
              )
            {
                uc_engineStopInRadio_mode += 1;
            }
            break;
        case 1:
            uc_device_stop_do = 1;
            uc_device_start_flag_do = 0;
            uc_engineStopInRadio_mode += 1;
            break;
        case 2:
            if(((uc_StopInCmd == 0) && (uc_mb_rtu_engineStopIn == 0)) ||
               (uc_diRadioChooseIn  == 0) || 
               (uc_diRemoteChooseIn == 1) 
              )
            {
                uc_device_stop_do = 0;
                uc_engineStopInRadio_mode = 0;
                tReturn = 1;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 前进控制，前进结束才能进入后退控制
 */
uint8_t uc_forward_ctrl = 0;
uint8_t user_forward_ctrl(void)
{
    uint8_t tReturn = 0;

    switch(uc_forward_ctrl)
    {
        case 0:
            if(uc_mdRtu_dialButton_2up == 1)
            {
                uc_forward_ctrl += 1;
            }
            break;
        case 1:
            us_forward_pwm_1 += 100;     //20ms任务中，每20ms加1次，如果任务放到20ms，则每次加2
            if(us_forward_pwm_1 > MAX_DUTY_VALUE)
            {
                us_forward_pwm_1 = MAX_DUTY_VALUE;
            }
            us_forward_pwm_2 += 100;
            if(us_forward_pwm_2 > MAX_DUTY_VALUE)
            {
                us_forward_pwm_2 = MAX_DUTY_VALUE;
            }
            if(((us_forward_pwm_1 == MAX_DUTY_VALUE) && (us_forward_pwm_2 == MAX_DUTY_VALUE)) || //已经加到最大
               (uc_mdRtu_dialButton_2up == 0)//前进关闭
              )
            {
                uc_forward_ctrl += 1;
            }
            break;
        case 2://前进中，等待前进结束
            if(uc_mdRtu_dialButton_2up == 0)//前进结束
            {
                uc_forward_ctrl += 1;
            }
            break;
        case 3://前进结束，2S内PWM减到0
            if(us_forward_pwm_1 > 100)
            {
                us_forward_pwm_1 -= 100;
            }
            else
            {
                us_forward_pwm_1 = 0;
            }
            if(us_forward_pwm_2 > 100)
            {
                us_forward_pwm_2 -= 100;
            }
            else
            {
                us_forward_pwm_2 = 0;
            }
            if((us_forward_pwm_2 == 0) && (us_forward_pwm_1 == 0))
            {
                uc_forward_ctrl = 0;
                tReturn = 1;
            }
            else if(uc_mdRtu_dialButton_2up == 1)//pwm未减到0时，前进再次按下
            {
                uc_forward_ctrl = 0;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 后退控制
 */
uint8_t uc_retreat_ctrl = 0;
uint8_t user_retreat_ctrl(void)
{
    uint8_t tReturn = 0;
    switch(uc_retreat_ctrl)
    {
        case 0:
            if(uc_mb_rtu_autoRetreatIn == 1)
            {
                uc_retreat_ctrl += 1;
            }
            break;
        case 1:
            us_retreat_pwm_1++;
            if(us_retreat_pwm_1 > 100)
            {
                us_retreat_pwm_1 = 100;
            }
            us_retreat_pwm_2++;
            if(us_retreat_pwm_2 > 100)
            {
                us_retreat_pwm_2 = 100;
            }
            if(((us_retreat_pwm_1 == 100) && (us_retreat_pwm_2 == 100)) ||
               (uc_mb_rtu_autoRetreatIn == 0)//后退关闭
              )
            {
                uc_retreat_ctrl += 1;
            }
            break;
        case 2:
            if(uc_mb_rtu_autoRetreatIn == 0)
            {
                uc_retreat_ctrl += 1;
            }
            break;
        case 3:
            if(us_retreat_pwm_1 > 1)
            {
                us_retreat_pwm_1--;
            }
            else
            {
                us_retreat_pwm_1 = 0;
            }

            if(us_retreat_pwm_2 > 1)
            {
                us_retreat_pwm_2--;
            }
            else
            {
                us_retreat_pwm_2 = 0;
            }
            if((us_retreat_pwm_2 == 0) && (us_retreat_pwm_1 == 0))
            {
                uc_retreat_ctrl = 0;
                tReturn = 1;
            }

            if(uc_mb_rtu_autoRetreatIn == 1)//在后退过程中，后退再次启动
            {
                uc_retreat_ctrl = 0;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 遥控模式下，用户逻辑处理;
 * 前进：0->1,前进阀的PWM在2S内从0->100；1->0，前进阀的PWM从当前值匀速到0；
 * 这个时候，用户如果把拨钮拨到了下，则暂时不响应，等前进阀的PWM从当前值降到0之后，再去响应；
 */
work_mode_t uc_workInRadio_type = eIsStart;
uint8_t user_workInRadio(void)
{
    uint8_t temp = 0;
    uint8_t tReturn = 0;

    switch(uc_workInRadio_type)
    {
        case eWorkNothing:
            if((uc_alarm_sn == 0) && (uc_mdRtu_enmergency_stop == 0))
            {
                if((uc_StartInCmd == 1) || (uc_mb_rtu_engineStartIn == 1))
                {
                  uc_workInRadio_type = eEngineStart;   
                }
            }
            if(uc_diRadioChooseIn == 0)//取消遥控模式
            {
                tReturn = 2;
            }
            break;
        case eEngineStart:
            temp = engine_startInRadio();
            if (temp == 1)
            {
                uc_workInRadio_type = eIsStart;
            }
            else if(temp == 2)//故障或者急停
            {
                uc_workInRadio_type = eEmorgency;
            }
            break;
        case eEngineStop:
            temp = engine_stopInRadio();
            if (temp == 1)
            {
                uc_workInRadio_type = eWorkNothing;
            }
            break;
        case eIsStart:
            if(uc_device_start_flag_do == 1)//启动标志
            {
                uc_workInRadio_type = eFanIsOpen;
            }
            else
            {
                uc_preActive_warning_do = 0;
                us_forward_pwm_1 = 0;
                us_retreat_pwm_1 = 0;
                us_forward_pwm_2 = 0;
                us_retreat_pwm_2 = 0;
                us_brake_pwm     = 0;

                us_fan_pwm = 0; 

                uc_workInRadio_type = eEngineStop;
            }
            break;
        case eFanIsOpen:
            if(us_fan_pwm == MAX_DUTY_VALUE)//风扇已经打开
            {
                uc_workInRadio_type = eChooseWorkMode;
            }
            if(uc_device_start_flag_do == 0)
            {
                uc_workInRadio_type = eEngineStop;
            }
            break;
        case eChooseWorkMode:
            if(uc_mdRtu_dialButton_2up == 1)        //自动前进
            {
                uc_workInRadio_type = eAutoForward;
            }
            else if((uc_mb_rtu_fastForwardIn > 0) ||//手动前进
                    (uc_mb_rtu_slowForwardIn > 0) 
                   )
            {
                uc_workInRadio_type = eHandForward;
            }
            else if(uc_mb_rtu_autoRetreatIn == 1)   //自动后退
            {
                uc_workInRadio_type = eAutoRetreat;
            }
            else if((uc_mb_rtu_fastRetreatIn > 0) ||//手动后退
                    (uc_mb_rtu_slowRetreatIn > 0)
                   )
            { 
                uc_workInRadio_type = eHandRetreat;
            }
            
            if((uc_mb_rtu_fanIn         == 0) || 
               (uc_device_start_flag_do == 0) ||
               (uc_diRadioChooseIn      == 0) 
              )
            {
                us_fan_pwm = 0;
                uc_preActive_warning_do = 0;
                us_forward_pwm_1 = 0;
                us_retreat_pwm_1 = 0;
                us_forward_pwm_2 = 0;
                us_retreat_pwm_2 = 0;
                us_brake_pwm     = 0;
  
                uc_workInRadio_type = eEngineStop;
            }
            break;
        case eAutoForward:
            temp = user_forward_ctrl();
            if(temp == 1)
            {
                uc_workInRadio_type = eChooseWorkMode;
            }
            break;
        case eAutoRetreat:
            temp = user_retreat_ctrl();
            if(temp == 1)
            {
                uc_workInRadio_type = eChooseWorkMode;
            }
            break;
        case eHandForward:
            if(uc_mb_rtu_fastForwardIn > 0)
            {
                us_forward_pwm_1 = uc_mb_rtu_fastForwardIn;
                us_forward_pwm_2 = uc_mb_rtu_fastForwardIn;
            }
            else if(uc_mb_rtu_slowForwardIn > 0)
            {
                us_forward_pwm_1 = uc_mb_rtu_slowForwardIn;
            }
            else
            {
                us_forward_pwm_1 = 0;
                us_forward_pwm_2 = 0;
                uc_workInRadio_type = eChooseWorkMode;
            }
            break;
        case eHandRetreat:
            if(uc_mb_rtu_fastRetreatIn > 0)
            {
                us_retreat_pwm_1 = uc_mb_rtu_fastRetreatIn;
                us_retreat_pwm_2 = uc_mb_rtu_fastRetreatIn;
            }
            else if(uc_mb_rtu_slowRetreatIn > 0)
            {
                us_retreat_pwm_1 = uc_mb_rtu_slowRetreatIn;
            }
            else
            {
                us_retreat_pwm_1 = 0;
                us_retreat_pwm_2 = 0;

                uc_workInRadio_type = eChooseWorkMode;
            }
            break;
        case eEmorgency:
            if((uc_alarm_sn == 1) || (uc_mdRtu_enmergency_stop == 1) || (uc_mdRtu_online == 0))
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 1;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/
            }
            else
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 0;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/

                uc_workInRadio_type = eWorkNothing;
            }

            if(uc_diRadioChooseIn == 0)//取消了遥控模式
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 0;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/

                uc_workInRadio_type = eWorkNothing;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 本地模式下，发动机启动
 * return 1-发动机启动完成；2-启动过程中异常或者启动按钮中途松开
*/
uint8_t uc_engineStartInLocal_mode = 0;
uint8_t engine_startInLocal(void)
{
    uint8_t tReturn = 0;
    switch(uc_engineStartInLocal_mode)
    {
        case 0:
            if(uc_alarm_sn == 0) 
            {
                if(uc_StartInCmd == 1) 
                {
                    Lib_Timer_Start(&uc_activateWarning_timer_start);
                    uc_engineStartInLocal_mode += 1;
                }
            }
            break;
        case 1:
            uc_device_start_do      = 1;
            uc_device_start_flag_do = 1;
            uc_activate_warning_do  = 1;
            if(ul_activateWarning_timer_cnt > TIME_OUT_1S)
            {
                Lib_Timer_Stop(&ul_activateWarning_timer_cnt,&uc_activateWarning_timer_start);
                uc_activate_warning_do = 0;
                uc_engineStartInLocal_mode += 1;
            }
            if(uc_StartInCmd == 0) 
            {
                Lib_Timer_Stop(&ul_activateWarning_timer_cnt,&uc_activateWarning_timer_start);
                uc_activate_warning_do = 0;
                uc_device_start_do     = 0;
                uc_device_start_flag_do= 0;
                uc_engineStartInLocal_mode = 0;
                tReturn = 2;
            }
            else if(uc_alarm_sn == 1) 
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 0;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/

                Lib_Timer_Stop(&ul_activateWarning_timer_cnt,&uc_activateWarning_timer_start);
                uc_engineStartInLocal_mode = 0;
                tReturn = 2;
            }
            break; 
        case 2: 
            if(uc_StartInCmd == 0)
            {
                uc_device_start_do  = 0;
                uc_engineStartInLocal_mode = 0;

                tReturn = 1;//启动完成
            }
            else if(uc_alarm_sn == 1)
            {
                uc_device_start_do      = 0;/*KA5-设备启动 */
                uc_device_stop_do       = 0;/*KA6-设备停止 */
                uc_device_start_flag_do = 0;/*KA7-启动标志位*/

                uc_engineStartInLocal_mode = 0;

                tReturn = 2;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 在本地模式下发动机停止
 * return 1-熄火；
 */
uint8_t uc_engineStopInLocal_mode = 0;
uint8_t engine_stopInLocal(void)
{
    uint8_t tReturn = 0;

    switch(uc_engineStopInLocal_mode)
    {
        case 0:
            if((uc_StopInCmd == 1) || (uc_diRadioChooseIn == 1) || (uc_diRemoteChooseIn == 1))
            {
                uc_engineStopInLocal_mode += 1;
            }
            break;
        case 1:
            uc_device_stop_do = 1;
            uc_device_start_flag_do = 0;
            if((uc_StopInCmd == 0) || (uc_diRadioChooseIn == 1) || (uc_diRemoteChooseIn == 1))
            {
                uc_device_stop_do = 0;
                uc_engineStopInLocal_mode = 0;
                tReturn = 1;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 本地工作模式的发动机启动和熄火
 */
work_mode_t uc_workInLocal_type = eEngineStart;
uint8_t user_workInLocal(void)
{
    uint8_t temp = 0;
    uint8_t tReturn = 0;

    switch(uc_workInLocal_type)
    {
        case eWorkNothing:
            if(uc_StartInCmd == 1) 
            {
                uc_workInLocal_type = eEngineStart;
            }
            else if(uc_diRadioChooseIn == 1)
            {
                tReturn = 2;
            }
            else if(uc_diRemoteChooseIn == 1) 
            {
                tReturn = 3;
            }
            break;
        case eEngineStart:
            temp = engine_startInLocal();
            if(temp == 1)
            {
                uc_workInLocal_type = eEngineStop;
            }
            else if(temp == 2)
            {
                uc_workInLocal_type = eWorkNothing;
            }
            break;
        case eEngineStop:
            temp = engine_stopInLocal();
            if(temp == 1)
            {
                uc_workInLocal_type = eWorkNothing;
            }
            break;
        default:
        break;
    }
    return tReturn;
}
/**
 * @brief 风扇控制
*/
void user_fan_crtl(void)
{
    if(uc_mb_rtu_fanIn == 1)
    {
        us_fan_pwm = MAX_DUTY_VALUE;
    }
    else
    {
        us_fan_pwm = 0;
    }
}
/**
 * @brief 刹车控制
 */
void user_brake_crtl(void)
{
    if((us_forward_pwm_1 != 0) ||
       (us_forward_pwm_2 != 0) ||
       (us_retreat_pwm_1 != 0) ||
       (us_retreat_pwm_2 != 0)
      )
    {
        us_brake_pwm = 100;
    }
    else
    {
        us_brake_pwm = 0;
    }
}
/**
 * @brief 逻辑运算，用户工作模式处理
 */
work_in_mode_t uc_workMode = eWorkInLocal;
void user_work_mode(void)
{
    uint8_t temp = 0;

    user_fan_crtl();/*风扇控制 */
    user_brake_crtl();/*刹车控制*/

    switch(uc_workMode)
    {
        case eWorkInLocal:
            temp = user_workInLocal();
            if(temp == 2)
            {
                uc_workMode = eWorkInRadio;
            }
            else if(temp == 3)
            {
                uc_workMode = eWorkInRemote;
            }
            break;
        case eWorkInRadio:
            temp = user_workInRadio();
            if(temp == 2)
            {
                uc_workMode = eWorkInLocal;
            }
            break;
        case eWorkInRemote:
            break;
        default:
        break;
    }
}
/**
 * @brief 用户应用程序，放在20ms任务中
 */
void user_app_run(void)
{
    uc_rd_dataHandler();/*自由协议数据解析*/
    di_data_redefine();/*DI输入解析和数据重定义*/
    user_alarm_handler();/*传感器报警处理*/
    m_modbus_rtu_dataAnalysis();/*mobus rtu数据解析 */
    rocker_dataHandler();/*modbus rtu摇杆解析 */
    m_modbus_rtu_dataRedefine();/*modbus rtu数据重定义*/
    /*逻辑运算*/
    user_work_mode();
    /*输出控制*/
    out_redefine();
}
/**
 * @brief 用户CAN发送，放在100ms任务中
 */
void user_app_can_run(void)
{
    uint32_t id = 0;
    uint8_t buf[8] = { 0 };
    uint8_t temp[8] = { 0 };

    id = 0x180;
    Lib_FloatToU8(uf_gasConcentration,&buf[0]);
    Lib_FloatToU8(uf_hydraulicOilTemperature,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x181;
    memset(buf,0,8);
    Lib_FloatToU8(uf_engineWaterTemperature,&buf[0]);
    Lib_FloatToU8(uf_engineTemperature,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x182;
    memset(buf,0,8);
    Lib_FloatToU8(uf_exhaustGasTemperature,&buf[0]);
    Lib_FloatToU8(uf_mainPumpPressure,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x183;
    memset(buf,0,8);
    Lib_FloatToU8(uf_fanPressure,&buf[0]);
    Lib_FloatToU8(uf_brakePressure,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x184;
    memset(buf,0,8);
    Lib_FloatToU8(uf_gasConcentration_alarmValue,&buf[0]);
    Lib_FloatToU8(uf_hydraulicOilTemperature_alarmValue,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x185;
    memset(buf,0,8);
    Lib_FloatToU8(uf_engineWaterTemperature_alarmValue,&buf[0]);
    Lib_FloatToU8(uf_engineTemperature_alarmValue,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x186;
    memset(buf,0,8);
    Lib_FloatToU8(uf_exhaustGasTemperature_alarmValue,&buf[0]);
    Lib_FloatToU8(uf_mainPumpPressure_alarmValue,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x187;
    memset(buf,0,8);
    Lib_FloatToU8(uf_fanPressure_alarmValue,&buf[0]);
    Lib_FloatToU8(uf_brakePressure_alarmValue,&buf[4]);
    Can0_Data_Store_Fifo(id,buf,8);

    id = 0x200;
    memset(buf,0,8);
    if(uf_gasConcentration > uf_gasConcentration_alarmValue)
    {
        temp[0] = 1;
    }
    else 
    {
        temp[0] = 0;
    }
    if(uf_hydraulicOilTemperature > uf_hydraulicOilTemperature_alarmValue)
    {
        temp[1] = 1;
    }
    else 
    {
        temp[1] = 0;
    }
    if(uf_engineWaterTemperature > uf_engineWaterTemperature_alarmValue)
    {
        temp[2] = 1;
    }
    else
    {
        temp[2] = 0;
    }
    if(uf_engineTemperature > uf_engineTemperature_alarmValue)
    {
        temp[3] = 1;
    }
    else
    {
        temp[3] = 0;
    }
    if(uf_exhaustGasTemperature > uf_exhaustGasTemperature_alarmValue)
    {
        temp[4] = 1;
    }
    else
    {
        temp[4] = 0;
    }
    if(uf_mainPumpPressure > uf_mainPumpPressure_alarmValue)
    {
        temp[5] = 1;
    }
    else
    {
        temp[5] = 0;
    }
    if(uf_fanPressure > uf_fanPressure_alarmValue)
    {
        temp[6] = 1;
    }
    else
    {
        temp[6] = 0;
    }
    if(uf_brakePressure > uf_brakePressure_alarmValue)
    {
        temp[7] = 1;
    }
    else
    {
        temp[7] = 0;
    }
    buf[0] = (temp[0]<<0) | (temp[1]<<1) | (temp[2]<<2) | (temp[3]<<3)|
             (temp[4]<<4) | (temp[5]<<5) | (temp[6]<<6) | (temp[7]<<7);

    buf[1] = (uc_diLowLiquidLevelDetectionFirstIn   <<0) |
             (uc_diLowLiquidLevelDetectionSecondIn  <<1) |
             (uc_diLowLiquidLevelDetectionFirstIn   <<2) |
             (uc_diLowLiquidLevelDetectionSecondIn  <<3) |
             (uc_fault_alarm_do                     <<4) |
             (uc_activate_warning_do                <<5) |
             (uc_preActive_warning_do               <<6) |
             (uc_device_start_flag_do               <<7);
    buf[2] = (uc_oilOut_flag_do     <<0) |
             (uc_mdRtu_online       <<1) |
             (uc_diRadioChooseIn    <<2) |
             (uc_diRemoteChooseIn   <<3) |
             (uc_mb_rtu_fanIn       <<4);
    Can0_Data_Store_Fifo(id,buf,8);
}



